package com.wjr.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjr.model.User;
import com.wjr.service.UserService;

public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	private UserService userService = null;
	private User user = null;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String mark = request.getParameter("mark");
		if("login".equals(mark)){
			try {
				login(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		userService = new UserService();
		//����login()�������е�¼��Ϣ��֤
		user = userService.login(username,password);
		
		if(null!=user){
			request.getRequestDispatcher("../frame.jsp").forward(request, response);	
		}else{
			request.getRequestDispatcher("../sign-in.jsp").forward(request, response);	
		}
	}

}

